package com.example.androidcomplete0102.ch12;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.androidcomplete0102.R;
import com.example.androidcomplete0102.ch12.CustomAdapterView.MyItem;

public class MyCustomAdapter extends BaseAdapter {
	String TAG = "MyListAdapter";
	LayoutInflater inflate;
	Context mContext;
	ArrayList<MyMenu> arLists;
	int layoutRes;
	
	public MyCustomAdapter(Context context, int aResLayout, ArrayList<MyMenu> lists) {
		//외부클래스이기 때문에 어댑터를 생성하는 곳에서 인자를 전달 받아야 한다. 
		mContext = context;
		arLists = lists;
		layoutRes = aResLayout;
		inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		Log.d(TAG, "getCount()");
		return arLists.size();
	}

	@Override
	public Object getItem(int position) {
		Log.d(TAG, "getItem("+ position + ")");
		return arLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		Log.d(TAG, "getItemId("+ position + ")");
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;
		Log.d(TAG, "getView("+ position + ")");
		if(convertView == null){
			convertView = inflate.inflate(layoutRes, parent, false);
		}
		ImageView img = (ImageView)convertView.findViewById(R.id.img);
		img.setImageResource(arLists.get(position).iconRes);
		
		TextView tv= (TextView)convertView.findViewById(R.id.text);
		tv.setText(arLists.get(position).name);
		
		Button btn = (Button)convertView.findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String text = arLists.get(pos).name+"를 출력합니다.";
				Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
			}
		});
		return convertView;
	}
}
